package com.facedemo.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.facedemo.MyApplication;
import com.facedemo.R;
import com.facedemo.db.entity.Person;
import com.facedemo.db.utils.PersonDaoUtils;
import com.facedemo.face.FaceInfoManger;
import com.facedemo.utils.ImageUtils;

import java.util.List;

public class PersonAdapter extends ArrayAdapter<Person> {
    private List<Person> mList;

    public PersonAdapter(@NonNull Context context, @LayoutRes int resource) {
        super(context, resource);
        mList = PersonDaoUtils.loadAll();
    }

    @Override
    public int getCount() {
        if (null == mList) {
            return 0;
        }
        return mList.size();
    }

    @Nullable
    @Override
    public Person getItem(int position) {
        if (null == mList) {
            return null;
        }
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        if (null == mList) {
            return 0;
        }
        return position;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        final Person info = getItem(position);
        ViewHolder viewHolder;
        if (null == convertView) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_user, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.id = (TextView) convertView.findViewById(R.id.item_user_id);
            viewHolder.name = (TextView) convertView.findViewById(R.id.item_user_name);
            viewHolder.head = (ImageView) convertView.findViewById(R.id.item_user_head);
            viewHolder.del = (ImageView) convertView.findViewById(R.id.item_user_del);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.id.setText("ID : " + info.getPersonId());
        viewHolder.name.setText("NAME : " + info.getName());
        String path = MyApplication.DIR_PATH + info.getPersonId() + "_1.jpg";
        if (null != path && 0 < path.length()) {
            Bitmap bt = ImageUtils.getBitmap(path,48,64);
            viewHolder.head.setImageBitmap(bt);
        }

        viewHolder.del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDel(info);
            }
        });
        return convertView;
    }

    class ViewHolder {
        ImageView head, del;
        TextView id, name;
    }

    private void showDel(final Person info) {
        new AlertDialog.Builder(getContext())
                .setMessage("是否删除" + info.getName() + "的数据")
                .setTitle("删除注冊用戶信息")
                .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //从数据库中删除人员信息+人脸信息
                        PersonDaoUtils.deleteById(info.getPersonId(),true);
                        //从内存的人脸库中删除
                        FaceInfoManger.getInstance().deleteFace(info.getPersonId());

                        mList = PersonDaoUtils.loadAll();
                        dialog.dismiss();
                        notifyDataSetChanged();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();
    }
}
